package com.cms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cms.common.QueryPageParam;
import com.cms.common.Result;
import com.cms.entity.Goods;
import com.cms.entity.Record;
import com.cms.entity.User;
import com.cms.entity.req.RecordReq;
import com.cms.service.GoodsService;
import com.cms.service.RecordService;
import com.cms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.time.LocalDateTime;



@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private UserService userService;

    //分页查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody RecordReq recordReq) {
        Page<Record> page = new Page<>();
        page.setCurrent(recordReq.getPageNum());
        page.setSize(recordReq.getPageSize());

        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        if (recordReq.getCount() != null) {
            queryWrapper.eq("count", recordReq.getCount());//精准查询
        }
        if (recordReq.getId() != null) {
            queryWrapper.eq("id", recordReq.getId());//精准查询
        }
        if (recordReq.getGoodsId() != null) {
            queryWrapper.eq("goods_id", recordReq.getGoodsId());//精准查询
        }
        if (recordReq.getUserId() != null) {
            queryWrapper.eq("user_id", recordReq.getUserId());//精准查询
        }
        //这里的count、remark都是record的字段，方便前端用这几个字段体来查询，我写几个常用的
        if (recordReq.getRemark() != null) {
            queryWrapper.like("remark", recordReq.getRemark());//模糊查询
        }
        if (recordReq.getTime() != null) {
            queryWrapper.like("time", recordReq.getTime());//模糊查询
        }
        if (recordReq.getUserId() != null) {
            queryWrapper.eq("user_id", recordReq.getUserId());//精准查询
        }

        IPage<Record> result = recordService.pageCC(page, queryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }

    /*
    @PostMapping("/listPage")
    public Result listPage(@RequestBody RecordReq recordReq) {
        Page<Record> page = new Page<>();
        page.setCurrent(recordReq.getPageNum());
        page.setSize(recordReq.getPageSize());

        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        if (recordReq.getCount() != null) {
            queryWrapper.eq("count", recordReq.getCount());//精准查询
        }
        if (recordReq.getId() != null) {
            queryWrapper.eq("id", recordReq.getId());//精准查询
        }
        if (recordReq.getGoodsId() != null) {
            queryWrapper.eq("goods_id", recordReq.getGoodsId());//精准查询
        }
        if (recordReq.getUserId() != null) {
            queryWrapper.eq("user_id", recordReq.getUserId());//精准查询
        }
        //这里的count、remark都是record的字段，方便前端用这几个字段体来查询，我写几个常用的
        if (recordReq.getRemark() != null) {
            queryWrapper.like("remark", recordReq.getRemark());//模糊查询
        }
        if (recordReq.getTime() != null) {
            queryWrapper.like("time", recordReq.getTime());//模糊查询
        }
        if (recordReq.getUserId() != null) {
            queryWrapper.eq("user_id", recordReq.getUserId());//精准查询
        }

        IPage<Record> result = recordService.pageCC(page, queryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }
*/


    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Record record) {
        Goods goods = goodsService.getById(record.getGoodsId());
        int n = record.getCount();
        //出库
        if ("2".equals(record.getAction())) {
            n = -n;
            record.setCount(n);
        }

        int num = goods.getCount() + n;
        goods.setCount(num);
        goodsService.updateById(goods);

        // 在保存之前手动设置 time 字段的值为当前时间
        record.setTime(LocalDateTime.now());

        // 获取管理员对象
        User admin = userService.getById(record.getAdminId());
        if (admin == null) {
            throw new IllegalArgumentException("Invalid admin ID");
        }

        // 判断管理员角色
        if (!"0".equals(admin.getRoleId())) {
            throw new IllegalArgumentException("Invalid admin role");
        }

        return recordService.save(record) ? Result.suc() : Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return recordService.removeById(id)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Record record){
        return recordService.updateById(record)?Result.suc():Result.fail();
    }
    @GetMapping("/list")
    public Result list(){
        List list = recordService.list();
        return Result.suc(list);
    }
}
